﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxCallback;
using System.Web.Security;
using System.Configuration;
using DevExpress.Web.ASPxClasses;

namespace TireService.Web.View.OperatorView
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                TireServiceDB db = new TireServiceDB();

                ddlServiceCar.DataSource = db.GetServiceCars();
                ddlServiceCar.ValueField = "ServiceCarId";
                ddlServiceCar.TextField = "Description";
                ddlServiceCar.DataBind();

                gvCustomerRating.DataSource = db.ServiceCarScheduleDetailRatings.Take(10).ToList();
                gvCustomerRating.DataBind();
            }

            LoadDayPlanBackLog(Convert.ToInt32(ddlServiceCar.Value));
        }

        /// <summary>
        /// Function that updates
        /// </summary>
        protected void ddlScheduleDate_Callback(object source, CallbackEventArgsBase e)
        {
            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();

            BindScheduleDate(db, Convert.ToInt32(e.Parameter));
        }

        public void BindScheduleDate(TireServiceDB db, int ServiceCarId) 
        {
            // TODO: Change date, to now when releasing
            ddlScheduleDate.DataSource = db.GetServiceCarSchedules(ServiceCarId, DateTime.Now.AddMonths(-5));
            ddlScheduleDate.ValueField = "ScheduleDate";
            ddlScheduleDate.TextField = "ScheduleDate";
            ddlScheduleDate.DataBind();
        }

        public void LoadDayPlanBackLog(int ServiceCarId)
        {
            TireServiceDB db = new TireServiceDB();
            gvDayPlanBackLog.DataSource = db.ServiceCarScheduleDetailViews.Where(p => p.ScheduleDate <= DateTime.Now & p.CarId != null & p.ScheduleDetailTypeId == (int)Shared.ScheduleDetailType.ServiceDrive & p.WorkCompleted == false & p.ServiceCarId == ServiceCarId).OrderBy(p => p.ScheduleDate).ToList();
            gvDayPlanBackLog.DataBind();

            gvDayPlanBackLog.GroupBy(gvDayPlanBackLog.Columns["ScheduleDate"]);
            gvDayPlanBackLog.ExpandAll();
        }

        /// <summary>
        /// Postback function that updates overview of dayplan for a specific servicecar and scheduledate
        /// </summary>
        protected void gvDayPlanBackLog_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            // Get value (ServiceCarId)
            int ServiceCarId = Convert.ToInt32(e.Parameters);

            LoadDayPlanBackLog(ServiceCarId);
        }

        /// <summary>
        /// Gridview function that runs when databinding - Changes colors based on customerconfirmed value
        /// </summary>
        protected void gvDayPlanBackLog_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;

            int servicetype = Convert.ToInt32(gvDayPlanBackLog.GetRowValuesByKeyValue(e.KeyValue, "ScheduleDetailTypeId"));

            if (servicetype == (int)Shared.ScheduleDetailType.Available)
            {
                e.Row.BackColor = System.Drawing.Color.White;
            }
            if (servicetype == (int)Shared.ScheduleDetailType.Blocked)
            {
                e.Row.BackColor = System.Drawing.Color.Red;
            }
            if (servicetype == (int)Shared.ScheduleDetailType.ServiceDrive)
            {
                // Get customerconfirmed value - based on that rows will be formated
                bool CustomerConfirmed = Convert.ToBoolean(gvDayPlanBackLog.GetRowValuesByKeyValue(e.KeyValue, "CustomerConfirmed"));

                if (CustomerConfirmed)
                {
                    e.Row.BackColor = System.Drawing.Color.DarkGreen;
                }
                else
                {

                    e.Row.BackColor = System.Drawing.Color.Orange;
                }
                bool WorkCompleted = Convert.ToBoolean(gvDayPlanBackLog.GetRowValuesByKeyValue(e.KeyValue, "WorkCompleted"));
                if (WorkCompleted)
                {
                    e.Row.BackColor = System.Drawing.Color.DarkSlateBlue;
                }
            }
        }

        /// <summary>
        /// Callback function that handles completion of servicedrive
        /// </summary>
        protected void callMarkServiceCompleted_Callback(object sender, CallbackEventArgs e)
        {
            // Get value (servicecarscheduledetailid)
            int ServiceCarScheduleDetailId = Convert.ToInt32(e.Parameter);

            // Instantiate database
            TireServiceDB db = new TireServiceDB();

            // Get UserId
            Guid userid = new Guid(Membership.GetUser().ProviderUserKey.ToString());

            //string CustomerFeedbackURL = ConfigurationManager.AppSettings["CustomerFeedbackURL"].ToString();

            // Submit for each task (to avoid email being send out where database changes is not done)
            TaskBLL taskBLL = new TaskBLL();
            taskBLL.MarkServiceDriveAsCompletedByServiceCarScheduleDetailId(db, ServiceCarScheduleDetailId, true, false, true, userid, false);

            // Dispose database connection
            db.Dispose();
        }
    }
}